% 读取文件，设置保留原始列名
data = readtable('附件2.xlsx', 'VariableNamingRule', 'preserve');

% 提取自变量和因变量
X = table2array(data(:, {'体重(x1)', '腰围(x2)', '脉搏(x3)'}));
y1 = table2array(data(:, '单杠(y1)'));
y2 = table2array(data(:, '弯曲(y2)'));
y3 = table2array(data(:, '跳高(y3)'));

% 添加截距项
X = [ones(size(X, 1), 1), X];

% 分别对每个因变量进行最小二乘法回归
models = cell(3, 1);
for i = 1:3
    if i == 1
        y = y1;
    elseif i == 2
        y = y2;
    else
        y = y3;
    end
    
    % 计算回归系数
    beta = (X' * X) \ (X' * y);
    models{i} = beta;
end

% 输出回归系数和截距
coefficients = cell(3, 1);
intercepts = zeros(3, 1);
for i = 1:3
    coefficients{i} = models{i}(2:end);
    intercepts(i) = models{i}(1);
end

fprintf('回归系数：\n');
for i = 1:3
    fprintf('模型%d： ', i);
    fprintf('%.4f ', coefficients{i});
    fprintf('\n');
end

fprintf('\n截距：\n');
for i = 1:3
    fprintf('模型%d： %.4f\n', i, intercepts(i));
end

% 评估模型
for i = 1:3
    if i == 1
        y = y1;
    elseif i == 2
        y = y2;
    else
        y = y3;
    end
    
    y_pred = X * models{i};
    SST = sum((y - mean(y)).^2);
    SSE = sum((y - y_pred).^2);
    r2 = 1 - SSE/SST;
    mse = mean((y - y_pred).^2);
    
    fprintf('\n模型%d的评估指标：\n', i);
    fprintf('R² 分数：%.4f\n', r2);
    fprintf('均方误差：%.4f\n', mse);
end